// ---------------------------------------------------------------------
//
// -- 常用Action库
// -- author £
//
// ---------------------------------------------------------------------

let ActionLib = {
    FADE_HIDE_ACTION: cc.fadeTo(0.2, 0),
    SCENE_HIDE_ACTION: cc.fadeTo(0.01, 0),
    FADE_SHOW_ACTION: cc.fadeTo(0.3, 255),
    SCENE_SHOW_ACTION: cc.fadeTo(0.01, 255),
    FADE_SCALE_ACTION: cc.spawn(
        cc.scaleTo(0.3, 1, 1),
        cc.fadeTo(0.2, 255)
    ),

    FADE_SHOW: function (node, time, 
        delay, callFunc, field, data) {
        if (delay === undefined || delay === null)
            delay = 0.01;
        
        let arr = [];

        node.stopAllActions();
        arr[arr.length] = cc.delayTime(delay);
        arr[arr.length] = cc.fadeTo(time, 255);

        if (callFunc) {
            arr[arr.length] = cc.callFunc(callFunc, field, data);
        }

        node.runAction(
            cc.sequence(arr)
        );
    },

    FADE_HIDE: function (node, time, 
        delay, callFunc, field, data, useDefuaultCall) {
        if (delay === undefined || delay === null)
            delay = 0.01;
        
        let arr = [];

        node.stopAllActions();
        arr[arr.length] = cc.delayTime(delay);
        arr[arr.length] = cc.fadeTo(time, 0);

        if (useDefuaultCall) {
            callFunc = this._defaultHideEnd;
            field = this;
        }
        
        if (callFunc) {
            arr[arr.length] = cc.callFunc(callFunc, field, data);
        }

        node.runAction(
            cc.sequence(arr)
        );
    },

    SCALE: function (node, time, 
        startX, startY, endX, endY,
        delay, callFunc, field, data) {
        if (startX === undefined || startX === null)
            startX = node.scaleX;
        if (endX === undefined || endX === null) 
            endX = 1;
        if (startY === undefined || startY === null)
            startY = node.scaleY;
        if (endY === undefined || endY === null)
            endY = 1;
        if (delay === undefined || delay === null)
            delay = 0.01;
        
        let arr = [];

        node.stopAllActions();
        arr[arr.length] = cc.delayTime(delay);
        arr[arr.length] = cc.scaleTo(0.01, startX, startY);
        arr[arr.length] = cc.scaleTo(time, endX, endY);

        if (callFunc) {
            arr[arr.length] = cc.callFunc(callFunc, field, data);
        }

        node.runAction(
            cc.sequence(arr)
        );
    },

    FADE_MOVE_SHOW: function (node, time, 
        startX, startY, endX, endY,
        delay, callFunc, field, data) {
        if (startX === undefined || startX === null)
            startX = node.x;
        if (endX === undefined || endX === null) 
            endX = node.x;
        if (startY === undefined || startY === null)
            startY = node.y;
        if (endY === undefined || endY === null)
            endY = node.y;
        if (delay === undefined || delay === null)
            delay = 0.01;
        
        let arr = [];

        node.stopAllActions();
        node.opacity = 1;
        node.x = startX;
        node.y = startY;
        arr[arr.length] = cc.delayTime(delay);
        arr[arr.length] = cc.spawn(
                cc.fadeTo(time, 255),
                cc.moveTo(time, endX, endY)
            );

        if (callFunc) {
            arr[arr.length] = cc.callFunc(callFunc, field, data);
        }

        node.runAction(
            cc.sequence(arr)
        );
    },

    FADE_MOVE_HIDE: function (node, time, 
        endX, endY, delay, callFunc, field, data, useDefuaultCall) {
        if (endX === undefined || endX === null) 
            endX = node.x;
        if (endY === undefined || endY === null)
            endY = node.y;
        if (delay === undefined || delay === null)
            delay = 0.01;

        let arr = [];
        
        arr[arr.length] = cc.delayTime(delay);
        arr[arr.length] = cc.spawn(
                cc.fadeTo(time, 0),
                cc.moveTo(time, endX, endY)
            );

        if (useDefuaultCall) {
            callFunc = this._defaultHideEnd;
            field = this;
        }

        if (callFunc) {
            arr[arr.length] = cc.callFunc(callFunc, field, data);
        }

        node.stopAllActions();
        node.runAction(
            cc.sequence(arr)
        );
    },

    FADE_SCALE_SHOW: function (node, time, 
        startX, startY, endX, endY,
        delay, callFunc, field, data) {
        if (startX === undefined || startX === null)
            startX = 0;
        if (startY === undefined || startY === null)
            startY = 0;
        if (endX === undefined || endX === null)
            endX = 1;
        if (endY === undefined || endY === null)
            endY = 1;
        if (delay === undefined || delay === null)
            delay = 0.01;
        
        let arr = [];

        node.stopAllActions();
        node.opacity = 1;
        node.scaleX = startX;
        node.scaleY = startY;
        arr[arr.length] = cc.delayTime(delay);
        arr[arr.length] = cc.spawn(
                cc.fadeTo(time, 255),
                cc.scaleTo(time, endX, endY)
            );

        if (callFunc) {
            arr[arr.length] = cc.callFunc(callFunc, field, data);
        }

        node.runAction(
            cc.sequence(arr)
        );
    },

    FADE_SCALE_HIDE: function (node, time, 
        endX, endY, startX, startY, delay, 
        callFunc, field, data, useDefuaultCall) {
        if (endX === undefined || endX === null) 
            endX = 0;
        if (endY === undefined || endY === null)
            endY = 0;
        if (startX === undefined || startX === null)
            startX = node.scaleX;
        if (startY === undefined || startY === null)
            startY = node.scaleY;
        if (delay === undefined || delay === null)
            delay = 0.01;

        let arr = [];
        
        node.scaleX = startX;
        node.scaleY = startY;
        arr[arr.length] = cc.delayTime(delay);
        arr[arr.length] = cc.spawn(
                cc.fadeTo(time, 0),
                cc.scaleTo(time, endX, endY)
            );

        if (useDefuaultCall) {
            callFunc = this._defaultHideEnd;
            field = this;
        }

        if (callFunc) {
            arr[arr.length] = cc.callFunc(callFunc, field, data);
        }

        node.stopAllActions();
        node.runAction(
            cc.sequence(arr)
        );
    },

    SHAKE: function (node, time, startDist, dist, isH, delay) {
        if (delay === undefined || delay === null)
            delay = 0.01;

        let arr = [];
        
        isH ? node.x = startDist : node.y = startDist;
        arr[arr.length] = cc.delayTime(delay);
        arr[arr.length] = cc.callFunc(
            function () { 
                node.runAction(
                    cc.repeatForever(
                        cc.sequence(
                            cc.moveTo(time, isH ? startDist + dist : node.x,
                                !isH ? startDist + dist : node.y),
                            cc.moveTo(time, isH ? startDist : node.x,
                                !isH ? startDist : node.y),
                        )
                    )
                );
            }, this);

        node.stopAllActions();
        node.runAction(
            cc.sequence(arr)
        );
    },

    showAndDelayHide: function (node, showTime, hideTime, delay) {
        if (showTime === null || showTime === undefined)
            showTime = 0.3;
        if (hideTime === null || hideTime === undefined)
            hideTime = 0.3;
        if (delay === null || delay === undefined)
            delay = 2;

        this.FADE_SHOW(node, showTime);
        require('CommonUtil').delay(node, delay,
            function () {
                this.FADE_HIDE(node, hideTime, 
                    null, null, this, null, true);
            }, this);
    },

    _defaultHideEnd: function (target, data) {
        target.active = false;
    },
};

module.exports = ActionLib; 